<?php
require_once dirname(dirname(dirname(__FILE__))) . '/init.php';
/**
 * 根据日志表(p_product_sales_log)计算统计销量
 */
class CountSales extends Controller_CronJob {
	// 分页
	private $limit = 5000;
	/**
	 * 执行函数
	 */
	public function run() {
		$this->statistics();
	}
	/**
	 * 写入操作
	 */
	private function statistics() {
		$start = time();
		// 数据库
		$db = Model_Db::getInstance();
		$wdb = Model_Db::getInstance("write_db");
		$model = new Model_Base();
		$count_sql = '
			SELECT
				COUNT(1) as total
			FROM
				(
					SELECT
						goods_sn,
						warehouse_code
					FROM
						p_product_sales_log
					GROUP BY
						goods_sn,
						warehouse_code
				) AS temps
		';
		$count = $db->fetchOne($count_sql);
		$page = ceil($count/$this->limit);
		try {
			// 清表
			$wdb->query("truncate p_product_sales_statistics");
			for ($i = 0; $i < $page; $i++) {
				$sql = '
					SELECT
						goods_sn,
						warehouse_code,
						real_sale_7,
						real_sale_15,
						real_sale_30,
					
					IF (
						(
							max_7 > real_sale_7 * 3 / 7 && max_7 > 6
						),
						round((real_sale_7 - max_7) * 7 / 6),
						real_sale_7
					) AS sale_7,
					
					IF (
						(
							max_15 > real_sale_15 * 3 / 15 && max_15 > 6
						),
						round(
							(real_sale_15 - max_15) * 15 / 14
						),
						real_sale_15
					) AS sale_15,
					
					IF (
						(
							max_30 > real_sale_30 * 3 / 30 && max_30 > 6
						),
						round(
							(real_sale_30 - max_30) * 30 / 29
						),
						real_sale_30
					) AS sale_30
					FROM
						(
							SELECT
								id,
								goods_sn,
								warehouse_code,
								SUM(
					
									IF (
										sold_date >= SUBDATE(CURRENT_DATE, 7),
										sold_amount,
										0
									)
								) AS real_sale_7,
								SUM(
					
									IF (
										sold_date >= SUBDATE(CURRENT_DATE, 15),
										sold_amount,
										0
									)
								) AS real_sale_15,
								SUM(
					
									IF (
										sold_date >= SUBDATE(CURRENT_DATE, 30),
										sold_amount,
										0
									)
								) AS real_sale_30,
								MAX(
					
									IF (
										sold_date >= SUBDATE(CURRENT_DATE, 7),
										sold_amount,
										0
									)
								) AS max_7,
								MAX(
					
									IF (
										sold_date >= SUBDATE(CURRENT_DATE, 15),
										sold_amount,
										0
									)
								) AS max_15,
								MAX(
					
									IF (
										sold_date >= SUBDATE(CURRENT_DATE, 30),
										sold_amount,
										0
									)
								) AS max_30
							FROM
								p_product_sales_log
							GROUP BY
								goods_sn,
								warehouse_code
							ORDER BY
								id
							LIMIT '.($i*$this->limit).','.($this->limit).'
						) AS temps
				';
				$result = $db->fetchAll($sql);
				$wdb->beginTransaction();
				$model->insertBatch($wdb, 'p_product_sales_statistics', $result);
				// 提交事务
				$wdb->commit();
			}
		}catch (Exception $e) {
			$wdb->rollBack();
			exit($e->getMessage());
		}
		exit('success: '.(time() - $start));
	}
}
$countController = new CountSales();
$countController->run();